/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.databox.conversion;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.IOException;
import java.util.Iterator;

import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import twaver.AlarmSeverity;
import twaver.EllipseGroup;
import twaver.Follower;
import twaver.Link;
import twaver.Node;
import twaver.SubNetwork;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.network.TNetwork;
import twaver.network.background.ImageBackground;
import twaver.network.background.TextureBackground;
import demo.DemoPane;

public class ConversionDemo extends DemoPane {
    TDataBox box1 = new TDataBox();
    TNetwork network1 = new TNetwork(box1);
    TNetwork network2 = new TNetwork();
    TNetwork network3 = new TNetwork();

    public ConversionDemo() {
    	this.initBox1();   	
    	
    	network1.setToolbarByName(TWaverConst.DEFAULT_TOOLBAR);
    	network2.setToolbarByName(TWaverConst.DEFAULT_TOOLBAR);    	
    	network3.setToolbarByName(TWaverConst.DEFAULT_TOOLBAR);
    	
    	network1.clearMovableFilters();
    	network2.clearMovableFilters();
    	network3.clearMovableFilters();
    	
    	network1.setEnableDeleteWithKeyboard(true);
    	network2.setEnableDeleteWithKeyboard(true);
    	network3.setEnableDeleteWithKeyboard(true);
   
    	
    	final JTabbedPane tabbedPane = new JTabbedPane();
    	tabbedPane.add("Created By API", network1);
    	tabbedPane.add("Created By XML", network2);
    	tabbedPane.add("Created By Serialization", network3);
      	this.setLayout(new BorderLayout());
    	this.add(tabbedPane);
    	
    	tabbedPane.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent e) {
				int index = tabbedPane.getSelectedIndex();
				if(index == 1 && network2.getDataBox().size() == 0){
			    	initBox2();
			    	network2.getZoomer().zoomToOverview();
				}
				else if(index == 2 && network3.getDataBox().size() == 0){
			    	initBox3();
			    	network3.getZoomer().zoomToOverview();
				}
			}
    	});
    	
    	network1.getZoomer().zoomToOverview();
    }
    
    private void initBox1(){    	
    	this.box1.setBackground(new TextureBackground("/demo/resource/images/crystal.png"));
    	
    	SubNetwork sub = new SubNetwork();
    	sub.setLocation(1211, 472);
        ImageBackground background = new ImageBackground("/demo/resource/images/usa.gif");
        background.setColor(Color.black);
		background.setGradientColor(Color.cyan);
		background.setGradientFactory(TWaverConst.GRADIENT_EXTEND_S);
		background.setGradient(true);
		sub.setBackground(background);
    	this.box1.addElement(sub);
		
        Iterator it = AlarmSeverity.iterator();
        int x = 80;
        Node node = null;
        int i = 0;
        while(it.hasNext()){
        	i++;
        	Node lastNode = node;
        	// add node
        	AlarmSeverity severity = (AlarmSeverity)it.next();
        	node = new Node();
			node.setName(severity.getDisplayName());
			
        	node.setLocation(x, 50);
        	node.getAlarmState().addNewAlarm(severity);
        	box1.addElement(node);
    		
        	// add link
        	if(lastNode != null){
        		Link link = new Link(lastNode, node);
        		link.getAlarmState().addNewAlarm(severity);
                link.putLinkFlowing(true);
                link.putLinkFlowingColor(Color.black);
                link.putLinkColor(Color.white);
        		box1.addElement(link);
        	}
        	lastNode = node;
        	x += 120;
        }	
        
        
		// create 6 followers which host in loop.
        final int count = 6;
        for (i = 0; i < count; i++) {
            node = new Follower("node" + i);
            node.setLocation(400 + i * 35, 160);
            AlarmSeverity severity = AlarmSeverity.getByValue(i * 100);
            node.getAlarmState().addNewAlarm(severity);
            box1.addElement(node);
            if (i > 0) {
                ( (Follower) box1.getElementByID("node" + (i - 1))).setHost(node);
            }
        }
        Follower first = (Follower) box1.getElementByID("node0");
        Follower last = (Follower) box1.getElementByID("node" + (count - 1));
        last.setHost(first);

		EllipseGroup group = new EllipseGroup();
		group.setExpand(true);
		group.putGroupFillColor(new Color(0, 0, 152));
		group.putGroupEllipse3D(true);
		group.putGroupOpaque(true);
		box1.addElement(group);
		 
        final int r = 100;
        final int cx = 200; 
        final int cy = 260; 
        Node center = new Node();
        center.setImage("/demo/resource/images/loading.gif");
        center.setCenterLocation(cx, cy);
        box1.addElement(center);
        group.addChild(center);
        
        // create 5 followers which host center node.
        for (i = 1; i < count; i++) {
        	Follower follower = new Follower();
            x = (int) (r * Math.cos(Math.PI * 2 / (count-1) * i));
            int y = (int) (r * Math.sin(Math.PI * 2 / (count-1) * i));
            follower.setLocation(cx + x, cy + y);
            follower.setHost(center);
            follower.setImage("/demo/resource/images/loading.gif");
            AlarmSeverity severity = AlarmSeverity.getByValue(i * 100);
            follower.getAlarmState().addNewAlarm(severity);
            box1.addElement(follower);
            group.addChild(follower);
            Link link = new Link(follower, center);
            link.getAlarmState().addNewAlarm(severity);
            box1.addElement(link);
            group.addChild(link);
        }
        
        node.setSelected(true);
        node.setName("<html>In case you thought that TWaver label had to be" +
                "<p>boring, one line descriptions, the " +
                "<p><font color=blue size=+2>TWaver Team</font>" +
                "<p> is happy to shatter your illusions.<p>" +
                "In TWaver, they can use HTML to " +
                "<ul><li>Have Lists<li><b>Bold</b> " +
                "text<li><em>emphasized</em> " +
                "text<li>text with <font color=red>Color</font>" +
                "<li>text in different <font size=+3>sizes</font>" +
                "<li>and <font face=AvantGarde>Fonts</font></ul>" +
                "Oh, and they can be multi-line, too.</html>");
        
        try {
			box1.parse("/demo/databox/conversion/chassis.xml");		
			box1.parse("/demo/databox/conversion/bts.xml");
			box1.parse("/demo/databox/conversion/group.xml");
			box1.parse("/demo/databox/conversion/drawing.xml");
			box1.parse("/demo/databox/conversion/bus.xml");
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
    private void initBox2(){
		try {
			TDataBox box2 = new TDataBox();
			box2.parseXML(box1.toXML());
	    	network2.setDataBox(box2);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    private void initBox3(){
		try {
			byte[] data = TWaverUtil.toByteByGZIP(box1);
			TDataBox box3 = (TDataBox)TWaverUtil.toObjectByGZIP(data);
	    	network3.setDataBox(box3);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    

}